/*
* @Author: fredlee
* @Date:   2017-05-23 10:41:56
* @Last Modified by:   Administrator
* @Last Modified time: 2017-10-30 12:58:45
*/

import Vue from 'vue';

const vm = new Vue()

function getMgoodsNum () {

	return Promise.all([vm.$http.get(vm.$CONSTANTS.APIGoods+'goods/goodsNum/getGoodsNumOfMobile'),
						vm.$http.get(vm.$CONSTANTS.APIGoods+'goods/goodsNum/getGoodsNumOfPC')
		]);
}

function getPcgoodsNum () {
		return Promise.all([vm.$http.get(vm.$CONSTANTS.APIOrder+'main/seller/order/querySellerOrderNums'),
						vm.$http.get(vm.$CONSTANTS.APIOrder+'main/seller/order/queryMainSellerOrderNums')
		]);
}


//*********商品管理*************************商品管理*************************商品管理****************
function getGoodsListbyMobilePc (params) {

	return vm.$http.post(vm.$CONSTANTS.P30005+'mainGoods/getSellerBizOfferLst',params,{
	    before(xhr) {
	        if (this.lastRequest) {
	            this.lastRequest.abort()
	        }
	        this.lastRequest = xhr
	    }
});
}
function getGoodsListbyMobile (params) {
	return vm.$http.get(vm.$CONSTANTS.APIGoods+'goods/selectgoodslist',{
    before(xhr) {
        if (this.lastRequest) {
            this.lastRequest.abort()
        }
        this.lastRequest = xhr
        //this.params = params;
    },
    params:params
});
}

function getGoodsListbyPc (params) {
	let status = null
	switch (Number(params.status)) {
		case 0: 
			status = 1
			break
		case 1: 
			status = 2
			break
		case 2:
			status = 4
			break
		case 4:
			status = 3
			break
	}
	return vm.$http.post(vm.$CONSTANTS.APIMGCoin+'seller/goods/getBizOfferListForSellerV2', {
		bizOfferStatus: status,
		pageIndex: params.page,
		pageSize: params.pageSize
	})
}
function online (params) {
	return vm.$http.post(vm.$CONSTANTS.APIGoods+'goods/online',params);
	//return vm.$http.post('/mobile-goods-service/rs/goods/online',params);
}
function deleteMoblieGoods (params) {
	return vm.$http.post(vm.$CONSTANTS.APIGoods+'goods/deleteGoods',params);
	//return vm.$http.post('/mobile-goods-service/rs/goods/deleteGoods',params);
}

function initMainGoods(params){
	vm.$http.post(vm.$CONSTANTS.APIMGCoin + 'seller/gameaccount/initEditAccount', {bizOfferId: params.goodsId}).then(res => {
		if (res.data.success) {
			let ele = res.data.data
			_turn(ele)
		}
		return res
	})
}

function bizofferUpdate(params){
	return vm.$http.post(vm.$CONSTANTS.P30005+'mainGoods/idbizofferupdatebycreator',params);
}

function changeidbizofferprice(params){
	return vm.$http.post(vm.$CONSTANTS.P30005+'mainGoods/changeidbizofferprice',params);
}

function onlinPc (params) {
	return vm.$http.post(vm.$CONSTANTS.APIMainGoods+'mainGoods/updatemaingoodsonline',params);
	//return vm.$http.post('/mobile-mainGoods-service/rs/mainGoods/updatemaingoodsonline',params);
}
function deletePcGoods (params) {
	return vm.$http.post(vm.$CONSTANTS.APIMGCoin + 'seller/goods/batchDeleteAccount', {
		bizOfferIds: [params.goodsId]
	});
	//return vm.$http.post('/mobile-mainGoods-service/rs/mainGoods/deletemaingoods',params);
}

function updatePcGoods (params) {
	return vm.$http.post(vm.$CONSTANTS.APIMainGoods+'mainGoods/updatemaingoods1',params);
	//return vm.$http.post('/mobile-mainGoods-service/rs/mainGoods/updatemaingoods1',params);
}

//*********订单管理*************************订单管理*************************订单管理****************
function getOrderListbyMobile (params) {
	return vm.$http.get(vm.$CONSTANTS.APIOrder+'seller/order/list',{

	    before(xhr) {
	        if (this.lastMRequest) {
	            this.lastMRequest.abort()
	        }
	        this.lastMRequest = xhr
	    },
	    params:params
});
}
function getOrderListPcbyMobile (params) {

  return vm.$http.post(vm.$CONSTANTS.APIMainGoods+'mainGoodsOrder/getSellerOrderNewList',
		params,{
      before(xhr) {
        if (this.lastPcRequest) {
          this.lastPcRequest.abort()
        }
        this.lastPcRequest = xhr
      },
		}
  );
}


function getOrderListbyPC (params) {
	return vm.$http.get(vm.$CONSTANTS.APIOrder+'main/seller/order/querySellerOrderList',{
	    before(xhr) {
	        if (this.lastRequest) {
	            this.lastRequest.abort()
	        }
	        this.lastRequest = xhr
	    },
	    params:params
	})
}

function getMobileDetail (params) {
	return vm.$http.get(vm.$CONSTANTS.APIOrder+'seller/order/'+params);
}

function getMobileDetailPc (params) {

  return vm.$http.get(vm.$CONSTANTS.APIMainGoods+'mainGoodsOrder/getIdSellerOrderDetail?orderId='+params);
}

function getPCDetail (params) {
	return vm.$http.post(vm.$CONSTANTS.APIMGCoin + 'seller/order/getSellerOrderDetail', params).then(res => {
		if (res.data.success) {
			_turn(res.data.data)
		}
		return res
	})
	// return vm.$http.get(vm.$CONSTANTS.APIOrder+'main/seller/order/querySellerOrderDetail',{params:params});
}

function openServicesQQ (params) {
	return vm.$http.post(vm.$CONSTANTS.APIOrder+'assignCustomServie/openServiceQQ',params);
}

function updatemaingoodsonline (params) {
	return vm.$http.post(vm.$CONSTANTS.APIMGCoin + 'seller/goods/batchDisposeAccount', {
		bizOfferIds: [params.goodsId]
	});
}

// 临时方法：对象字段名首字母小写转首字母大写
function _turn (element) {
	if (element === null || element.constructor === String || element.constructor === Number) {
		// 字符串暂不处理
	} else if (element.constructor === Array) {
		// 数组处理
		element.forEach( v => {
			_turn(v)
		})
	} else {
		// 对象处理
		for (let key in element) {
    		element[key.replace(/^\S/, s => s.toUpperCase())] = element[key]
    		 _turn(element[key])
    		delete element[key]
  		}
	}
}

export default{    
	getMgoodsNum,
	//商品管理
	getGoodsListbyMobile,
	getGoodsListbyPc,
	online,
	deleteMoblieGoods,
	deletePcGoods,
	initMainGoods,
	updatePcGoods,
	//订单管理
	getOrderListbyMobile,
  getOrderListPcbyMobile,
	getOrderListbyPC,
	getPcgoodsNum,
	getMobileDetail,
  getMobileDetailPc,
	getPCDetail,
	openServicesQQ,
	updatemaingoodsonline,
	getGoodsListbyMobilePc,
	bizofferUpdate,
	changeidbizofferprice
}
